#include "stdafx.h"
#include "ProfilerSection.h"
#include "Profiler.h"


//-----------------------------------------------------------------------------
//															CProfilerSection
//-----------------------------------------------------------------------------
CProfilerSection::CProfilerSection(ISystem* pSystem, const char* name)
: m_started(false)
, m_time(0)
, m_smoothed_time(0)
, m_start_time(0)
, m_name(name)
, m_call_counter(0)
{
	assert(pSystem);

	m_pTimer = pSystem->GetTimer();
	pSystem->GetProfiler()->RegisterSection(this);
}

void CProfilerSection::Begin()
{
	assert(!m_started);

	m_call_counter++;
	m_start_time = m_pTimer->GetTime(eTime_Instant);
	m_started = true;
}

void CProfilerSection::End()
{
	assert(m_started);

	m_time += m_pTimer->GetTime(eTime_Instant) - m_start_time;
	m_started = false;

	//static float val_rate = 0;
	//Smooth(m_smoothed_time, val_rate, m_time, 0.01f, 0.5f);
	m_smoothed_time = m_time;
}

uint CProfilerSection::GetCallsCount() const
{
	return m_call_counter;
}

float CProfilerSection::GetTime() const
{
	return m_call_counter ? m_smoothed_time : 0;
}

const char* CProfilerSection::GetName() const
{
	return m_name.c_str();
}

void CProfilerSection::ResetCallCounter()
{
	m_time = 0;
	m_call_counter = 0;
}

//-----------------------------------------------------------------------------
//												CProfilerSectionTrigger
//-----------------------------------------------------------------------------
CProfilerSectionTrigger::CProfilerSectionTrigger(CProfilerSection* section)
{
	assert(section);

	m_section = section;
	m_section->Begin();
}

CProfilerSectionTrigger::~CProfilerSectionTrigger()
{
	m_section->End();
}